package com.wbao.dianniu.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ScoreData;
import com.wbao.dianniu.listener.IScoreListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ScoreListManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 积分明细
 * <p>
 * Created by yejianjun on 2017/11/14.
 */

public class MyScoreActivity extends BaseActivity implements IScoreListListener {
    private PullToRefreshListView pullListView;
    private ListView listView;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private TextView noDataTV;
    private ScoreListManager manager;
    private ScoreAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_score_detail, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.score_detail));
        initView();
        requestData();

    }

    private void initView() {
        getRightTextButton().setText(getResources().getString(R.string.score_rule));
        getRightTextButton().setOnClickListener(this);
        noDataTV = (TextView) findViewById(R.id.no_data_tv);
        pullListView = (PullToRefreshListView) findViewById(R.id.score_pullListView);
        pullListView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        listView = pullListView.getRefreshableView();
        registerForContextMenu(listView);
    }

    private void requestData() {
        manager = new ScoreListManager(this);
        manager.addScoreListListener(this);
        adapter = new ScoreAdapter(this);
        adapter.addData(new ArrayList<ScoreData>());
        listView.setAdapter(adapter);
        reqData(0);
    }

    private void reqData(int page) {
        manager.scoreList(GlobalContext.getAccountId(), page * PAGE_COUNTS, PAGE_COUNTS);
    }

    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onRefresh() {
            // 上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        // 没有搜索结果 或者数据为空 上拉没有效果
        currentPage++;
        reqData(currentPage);
    }

    @Override
    public void onScoreListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onScoreListSuccess(List<ScoreData> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            adapter.addData(list);
            if (adapter.getCount() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeScoreListListener(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    class ScoreAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private List<ScoreData> mList;

        public ScoreAdapter(Context context) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (null == mList)
                mList = new ArrayList<ScoreData>();
        }

        public void addData(List<ScoreData> list) {
            if (null == list)
                return;
            for (int i = 0; i < list.size(); i++) {
                mList.add(list.get(i));
            }
        }

        public List<ScoreData> getList() {
            return mList;
        }

        @Override
        public int getCount() {
            if (null == mList)
                return 0;
            else
                return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            GridHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.score_list_item, null);
                holder = new GridHolder();
                holder.contentTV = (TextView) convertView.findViewById(R.id.score_item_content);
                holder.dateTV = (TextView) convertView.findViewById(R.id.score_item_date);
                holder.scoreTV = (TextView) convertView.findViewById(R.id.score_item_scores);
                convertView.setTag(holder);
            } else {
                holder = (GridHolder) convertView.getTag();
            }
            ScoreData data = mList.get(position);
            if (null != data) {
                int score = data.getScore();
                if (data.getScore() > 0) {
                    holder.scoreTV.setText("+" + score);
                } else {
                    holder.scoreTV.setText(score + "");
                }
                holder.contentTV.setText(data.getContent() == null ? "" : data.getContent());
                holder.dateTV.setText(data.getCreateDate() == null ? "" : data.getCreateDate());
            }
            return convertView;
        }


        private class GridHolder {
            public TextView contentTV;
            public TextView dateTV;
            public TextView scoreTV;
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.right_button:
                startActivity(new Intent(MyScoreActivity.this, ScoreRuleActivity.class));
                break;
            default:
                break;

        }
    }
}
